package com.zyh.system.supermarket.service.impl;

import com.zyh.system.supermarket.dao.impl.UserDaoImpl;
import com.zyh.system.supermarket.domain.UserInfo;
import com.zyh.system.supermarket.domain.UserState;
import com.zyh.system.supermarket.service.UserService;

import java.util.Random;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * @Author zyh
 * @Date 2024/8/3 14:39
 * @Description
 */

public class UserServiceImpl implements UserService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final UserDaoImpl USER_DAO = new UserDaoImpl();

    /**
     * 展示所有会员信息
     *
     * @param
     */
    @Override
    public void selectAll(TreeMap<String, UserInfo> map) {
        USER_DAO.selectAll(map);
    }

    /**
     * 删除指定id的会员信息
     *
     * @param
     */
    @Override
    public void deleteUserInfoById(TreeMap<String, UserInfo> map) {
        System.out.println("请输入要删除的会员ID号：");
        String delId = INPUT.next();
        UserInfo userInfo = USER_DAO.selectById(map, delId);
        if (userInfo != null) {
            System.out.println("您要删除的信息如下");
            System.out.println("用户ID\t账号\t卡类型\t积分\t卡片状态");
            System.out.println(delId + "\t" + userInfo.getUserName() + "\t" + userInfo.getCardType() + "\t" + userInfo.getScore() + "\t" + userInfo.getCardType());
            System.out.println("您确定要删除吗y/n");
            String confirm = INPUT.next();
            if ("y".equals(confirm)) {
                boolean flag = USER_DAO.deleteUserInfoById(map, delId);
                if (flag) {
                    System.out.println("删除成功！");
                    selectAll(map);
                } else {
                    System.out.println("删除失败！");
                }
            }
        } else {
            System.out.println("对不起，您要删除的会员ID号不存在，请核实后在删除！");
        }
    }

    /**
     * 添加会员信息
     *
     * @param map
     */
    @Override
    public void addUserInfo(TreeMap<String, UserInfo> map) {
        System.out.println("请输入用户账号");
        String newName = INPUT.next();
        System.out.println("请输入卡类型");
        String newCardType = INPUT.next();
        System.out.println("请输入会员积分");
        Integer newScore = INPUT.nextInt();
        UserInfo newUserInfo = new UserInfo(newName, newCardType, newScore, UserState.合法账户);
        Integer randomId=new Random().nextInt(9000)+1000;
        String id=randomId+"";
        //判断随机出来的id是否以及存在
        for (String key : map.keySet()) {
            if (key.equals(id)){
                System.out.println("添加失败，随机的id已经存在");
                return;
            }
        }
        USER_DAO.addUserInfo(map,id,newUserInfo);
        System.out.println("添加成功");
    }

    /**
     * 根据id修改对应信息的积分
     * @param map
     */
    @Override
    public void updateScore(TreeMap<String, UserInfo> map) {
        System.out.println("请输入要修改积分的会员id");
        String id = INPUT.next();
        UserInfo userInfo = USER_DAO.selectById(map, id);
        if (userInfo!=null){
            System.out.println("查询到的信息如下");
            System.out.println("用户ID\t账号\t卡类型\t积分\t卡片状态");
            System.out.println(id + "\t" + userInfo.getUserName() + "\t" + userInfo.getCardType() + "\t" + userInfo.getScore() + "\t" + userInfo.getCardType());
            while (true) {
                System.out.println("请输入修改后的积分");
                int newScore = INPUT.nextInt();
                if (newScore<0){
                    System.out.println("积分不能为负数");
                    continue;
                }
                boolean flag = USER_DAO.updateScore(map, id, newScore);
                if (flag){
                    System.out.println("修改积分成功");
                    System.out.println("用户ID\t账号\t卡类型\t积分\t卡片状态");
                    System.out.println(id + "\t" + userInfo.getUserName() + "\t" + userInfo.getCardType() + "\t" + userInfo.getScore() + "\t" + userInfo.getCardType());
                    return;
                }
            }
        }
        else {
            System.out.println("您输入的id不存在");
        }
    }
}
